import Vue from 'vue';
import VueRouter from 'vue-router';
const Login = () =>
    import ( /* webpackChunkName: "Login_Home_Welcome" */ '../components/login/Login.vue');

const Home = () =>
    import ( /* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue');
const Welcome = () =>
    import ( /* webpackChunkName: "Login_Home_Welcome" */ '../components/Welcome.vue');

const User = () =>
    import ( /* webpackChunkName: "User_Roles_Authority" */ '../components/users/User.vue');
const Roles = () =>
    import ( /* webpackChunkName: "User_Roles_Authority" */ '../components/rights/Roles.vue');
const Authority = () =>
    import ( /* webpackChunkName: "User_Roles_Authority" */ '../components/rights/Authority.vue');

const GoodsList = () =>
    import ( /* webpackChunkName: "GoodsList_AddGoods_EditGoods" */ '../components/goods/GoodsList.vue');
const AddGoods = () =>
    import ( /* webpackChunkName: "GoodsList_AddGoods_EditGoods" */ '../components/goods/AddGoods.vue');
const EditGoods = () =>
    import ( /* webpackChunkName: "GoodsList_AddGoods_EditGoods" */ '../components/goods/EditGoods.vue');

const Category = () =>
    import ( /* webpackChunkName: "Category_Param" */ '../components/goods/Category.vue');
const Param = () =>
    import ( /* webpackChunkName: "Category_Param" */ '../components/goods/Param.vue');

const Orders = () =>
    import ( /* webpackChunkName: "Orders_Reports" */ '../components/orders/Orders.vue');
const Reports = () =>
    import ( /* webpackChunkName: "Orders_Reports" */ '../components/reports/Reports.vue');


//修复重复路由报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
}

Vue.use(VueRouter);

const routes = [{
    path: '/login',
    component: Login
}, {
    path: '/home',
    name: 'home',
    component: Home,
    children: [{
        //用户列表路由
        path: '/users',
        component: User
    }, {
        //首页欢迎路由
        path: '/welcome',
        component: Welcome
    }, {
        //角色列表路由
        path: '/roles',
        component: Roles
    }, {
        //权限列表路由
        path: '/rights',
        component: Authority
    }, {
        //商品列表路由
        path: '/goods',
        component: GoodsList
    }, {
        //商品分类列表路由
        path: '/categories',
        component: Category
    }, {
        //分类参数路由
        path: '/params',
        component: Param
    }, {
        //添加商品路由
        path: '/addgoods',
        component: AddGoods
    }, {
        //订单列表路由
        path: '/orders',
        component: Orders
    }, {
        //数据报表路由
        path: '/reports',
        component: Reports
    }, {
        //编辑商品路由
        path: '/editgoods/:id',
        component: EditGoods,
        props: true
    }]
}];

const router = new VueRouter({
    routes
});

//配置路由全局前置守卫
router.beforeEach(function(to, from, next) {

    if (to.path === '/' || to.path === '/home' || to.path === '/home/') {
        return next('/welcome');
    }

    if (to.path === '/login') {
        return next();
    }

    if (!localStorage.getItem('token')) {
        return next('/login');
    }

    next();
});

export default router;